/*
 * GenerateReadme.java		1.00	2008. 02. 14
 *
 * Copyright (c) 2008 David.Kim(KJS). All Rights Reserved.
 * 
 * This software is the confidential and proprietary information
 * of David.Kim(KJS).  You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the license agreement
 * you entered into with David.Kim(KJS).
 */
package doit.gen.util;

import java.io.File;
import java.io.RandomAccessFile;
import java.util.List;
import java.util.Map;

import org.springframework.util.StringUtils;

import doit.framework.util.Config;
import doit.gen.domain.Column;

/**
 * <pre>
 * system      : 공통
 * menu        : 공통
 * source      : GenerateReadme.java
 * description : 테이블에 매핑되는 소스에 대한 Readme 파일 생성
 * </pre> 
 * @version
 * <pre> 
 * 1.0	2008. 02. 14  leewan  최초 작성
 * 1.1	
 * </pre>
 */

@SuppressWarnings("unchecked")
public class GenerateReadme implements BaseGenerate {
	
	private String auth = "";
	private String authDomain = "";
	
	public void generate( Map input, String tableName, List columns) throws Exception {
		String packageName = (String) input.get("packageName");
		String jspPath = (String) input.get("jspPath");
		String auth = (String) input.get("auth");
	    String basePath = Config.getString("doit.generator.path.base");
	    String parentPath = basePath + "/" + packageName;
		String childPath = Config.getString("doit.generator.path.readme");
		
		this.auth = auth + ".";
		this.authDomain = "/" + auth + "/";
		
		// 폴더 생성
		GeneratorUtil.makeDirectorys( basePath, packageName);
		
		// 폴더 생성
		GeneratorUtil.makeDirectorys( parentPath, childPath);

		// 클래스명 분석
		String className = GeneratorUtil.getClassName( tableName);
		
		// 내용 생성
		StringBuffer sb = new StringBuffer();
		
		// sql-map-config.xml
		sb.append( printSqlMapXML( packageName, className)).append("\n");

		// ibatis.xml
		sb.append( printIBatisXML( packageName, className)).append("\n");

		// application.xml
		sb.append( printApplicationXML( packageName, jspPath, className)).append("\n");

		// servlet.xml
		sb.append( printServletXML( className)).append("\n");
		
		// 파일 생성
		File file = GeneratorUtil.makeFile( parentPath + "/" + childPath + "/" + className + "-README.txt");
		
		// 파일에 쓰기
		RandomAccessFile raf = new RandomAccessFile( file, "rw");
		GeneratorUtil.write( raf, sb.toString());
		
		// 파일 닫기
		raf.close();
	}
	
    public String printSqlMapXML( String packageName, String className) {
        String folderPath = StringUtils.replace( packageName, ".", "/");
        StringBuffer sb = new StringBuffer();
        
        sb.append("[1] [PROJECT_NAME]-sql-map-config.xml 내용 추가 : <sqlMapConfig> sqlMap 추가").append("\n\n");
        sb.append("    <sqlMap resource=\"" + folderPath + "/dao/ibatis/maps/" + className + ".xml\"/>").append("\n");
        
        return sb.toString();
    }
    
    public String printIBatisXML( String packageName, String className) {
        String augumentName = GeneratorUtil.getArgumentName( className);
        StringBuffer sb = new StringBuffer();
        
        sb.append("[2] [PROJECT_NAME]-ibatis.xml 내용 추가 : <beans> bean 추가").append("\n\n");
        sb.append("    <bean id=\"" + auth + augumentName + "DAO\" class=\"" + packageName + ".dao.ibatis." + className + "DAOImpl\">").append("\n");
        sb.append("        <property name=\"sqlMapClient\" ref=\"sqlMapClient\"/>").append("\n");
        sb.append("    </bean>").append("\n");
        
        return sb.toString();
    }
	
    public String printApplicationXML( String packageName, String jspPath, String className) {
        String augumentName = GeneratorUtil.getArgumentName( className);
        StringBuffer sb = new StringBuffer();
        
        sb.append("[3] [PROJECT_NAME]-application.xml 내용 추가 : <beans> bean 추가").append("\n\n");
        sb.append("    <bean id=\"" + auth + augumentName + "Controller\" class=\"" + packageName + ".controller." + className + "Controller\">").append("\n");
        sb.append("        <property name=\"methodNameResolver\" ref=\"methodNameResolver\" />").append("\n");
        sb.append("        <!-- page Definition -->").append("\n");
        sb.append("        <property name=\"init\"       value=\"" + jspPath + className               + "List\" />").append("\n");
        sb.append("        <property name=\"pageList\"   value=\"" + jspPath + className               + "List\" />").append("\n");
        sb.append("        <property name=\"list\"       value=\"" + jspPath + className               + "List\" />").append("\n");
        sb.append("        <property name=\"view\"       value=\"" + jspPath + className               + "View\" />").append("\n");
        sb.append("        <property name=\"insertForm\" value=\"" + jspPath + className               + "Insert\" />").append("\n");
        sb.append("        <property name=\"insert\"     value=\"redirect:"  + GeneratorUtil.getArgumentName( className) + ".do\" />").append("\n");
        sb.append("        <property name=\"updateForm\" value=\"" + jspPath + className               + "Update\" />").append("\n");
        sb.append("        <property name=\"update\"     value=\"redirect:"  + GeneratorUtil.getArgumentName( className) + ".do\" />").append("\n");
        sb.append("        <property name=\"delete\"     value=\"redirect:"  + GeneratorUtil.getArgumentName( className) + ".do\" />").append("\n");
        sb.append("        <property name=\"deleteAll\"  value=\"redirect:"  + GeneratorUtil.getArgumentName( className) + ".do\" />").append("\n\n");
        
        sb.append("<!--    <property name=\"initMethod\" value=\"pageList\" />  -->").append("\n");
        sb.append("    </bean>").append("\n");
        sb.append("    <bean id=\"" + auth + augumentName + "Service\" class=\"" + packageName + ".service." + className + "ServiceImpl\" />").append("\n");

        
        return sb.toString();
    }	
	
	public String printServletXML( String className) {
		StringBuffer sb = new StringBuffer();
		
		sb.append("[4] [PROJECT_NAME]-servlet.xml 내용 추가 : <beans><bean id=\"urlMapping\"><property name=\"urlMap\"><map> entry 추가").append("\n\n");
		sb.append("    <entry key=\"" + authDomain + GeneratorUtil.getArgumentName( className) + ".do\" value-ref=\"" + auth + GeneratorUtil.getArgumentName( className) + "Controller\" />").append("\n");
		
		return sb.toString();
	}
	
	public String printImport(String packageName, String className) {
		return "";
	}
	
	public String printClass( String className, List columns) throws Exception {
		return "";
	}
	
	public String printField( Column column) throws Exception {
		return "";
	}

	public String printClassStart( String className) {
		return "";
	}
	
	public String printClassEnd() {
		return "";
	}
}
